<?php
namespace app\admin\model;

use think\Config;
use think\Db;
use think\Model;
use think\Session;

class GoodsModel extends Model
{
	/**
	*	商品列表
	*/
	public function getGoods($data)
	{
		$where = [];

		/* 商品分类名称 */
		$cid = empty($data['cid']) ? '' : $data['cid'];
		$cid = trim($cid);
		if(!empty($cid)){
			$where['g.cid'] = $cid;
		}

		/* 商品名称 */
		$g_name = empty($data['g_name']) ? '' : $data['g_name'];
		$g_name = trim($g_name);
		if(!empty($g_name)){
			$where['g.g_name'] = $g_name;
		}

		/* 展示地方 */
		$g_show = empty($data['g_show']) ? '' : $data['g_show'];
		$g_show = trim($g_show);
		if(!empty($g_show)){
			$where['g.g_show'] = $g_show;
		}

		/* 状态 */
		$g_status = empty($data['g_status']) ? '' : $data['g_status'];
		$g_status = trim($g_status);
		if(!empty($g_status)){
			$where['g.g_status'] = $g_status;
		}

		/* 商品类型 */
		$where['g_type'] = 2;

		$join = [

			['sz_category c','g.cid=c.id','left']
		];

		$result = Db::table('sz_goods')->alias('g')
				->join($join)
				->field('g.*,c.name')
				->where($where)
				->order('g.id DESC')
				->paginate(20,false,['query' => request()->param()]);

		return $result;
		
	}

	/**
	*	商品规格列表
	*/	
	public function getInfo($gid)
	{
		$result = Db::table('sz_info')->alias('i')
				->join('sz_goods g','i.gid=g.id','left')
				->field('i.*,g.g_unit')
				->where('gid',$gid)
				->paginate(20,false,['query' => request()->param()]);

		return $result;
	}

	/**
	*	商品规格
	*/	
	public function getOneInfos($gid)
	{
		$result = Db::table('sz_info')->where('gid',$gid)->select();

		return $result;
	}

	/**
	*	商品分类
	*/
	public function goodsCategory()
	{
		$result = Db::table('sz_category')
				->field('*,concat(`path`,`id`) as paths')
				->order('paths')
				->select();

		foreach($result as $key=>&$val){
			// 计算逗号出现的次数
			$num = substr_count($val['path'],',');
			// 根据逗号出现的次数，填空格
			$zwf = str_repeat('★|',$num - 1);

			$val['name'] = $zwf.$val['name'];

		}

		return $result;
	}

	/**
	*	添加商品
	*/
	public function addGoods($data)
	{

		$map = [];
		$cate = [];
		if($data){
			if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
		        $map['g_pic']['photos'] = [];
		        foreach ($data['photo_urls'] as $key => $url) {
		            $photoUrl = cmf_asset_relative_url($url);
		            array_push($map['g_pic']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
		        }
        	}

        	/* 商品表 */

        	// 将图片的数组转为json格式，方便保存到数据库
	        $map['g_pic']         = json_encode($map['g_pic']);
	        $map['cid']           = trim($data['cid']);
	        $map['g_name']        = trim($data['g_name']);
	        $map['g_cprice']      = trim($data['g_cprice']);
	        $map['g_oprice']      = trim($data['g_oprice']);
	        $map['g_show']        = trim($data['g_show']);
	        $map['g_description'] = trim($data['g_description']);
	        $map['g_content']     = trim($data['g_content']);
	        $map['g_status']      = trim($data['g_status']);
	        $map['g_unit']        = trim($data['g_unit']);
	        $map['g_type']        = 2;
	        $map['addtime']       = empty($data['addtime']) ? 0 : strtotime(trim($data['addtime']));

	        $res = Db::table('sz_goods')->insertGetId($map);

	        
	        /* 商品规格信息表 */

			for($i=1;$i<=$data['length'];$i++){
		    	$cate['i_name'] = $data['i_name'.$i];	
		    	$cate['i_price'] = $data['i_price'.$i];	
		    	$cate['i_number'] = $data['i_number'.$i];	
		    	$cate['i_commission'] = $data['i_commission'.$i];
		    	$cate['gid']  = $res;
		    	Db::table('sz_info')->insert($cate);	
		    }

	        return true;

		}else{
			return false;
		}
	}

	/**
	*	获取单个商品信息
	*/
	public function getOne($id)
	{
		$join = [

			['sz_category c','g.cid=c.id','left']
		];
		$result = Db::table('sz_goods')->alias('g')
				->join($join)
				->field('g.*,c.name')
				->where('g.id',$id)
				->find();

		return $result;
	}



	/**
	*	修改商品信息提交
	*/	
	public function editGoods($id,$data)
	{
		$map = [];
		$cate = [];
		if($data){
			if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
		        $map['g_pic']['photos'] = [];
		        foreach ($data['photo_urls'] as $key => $url) {
		            $photoUrl = cmf_asset_relative_url($url);
		            array_push($map['g_pic']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
		        }
        	}

        	/* 商品表 */
        	// 将图片的数组转为json格式，方便保存到数据库
	        $map['g_pic']         = json_encode($map['g_pic']);
	        $map['cid']           = trim($data['cid']);
	        $map['g_name']        = trim($data['g_name']);
	        $map['g_cprice']      = trim($data['g_cprice']);
	        $map['g_oprice']      = trim($data['g_oprice']);
	        $map['g_show']        = trim($data['g_show']);
	        $map['g_description'] = trim($data['g_description']);
	        $map['g_content']     = trim($data['g_content']);
	        $map['g_status']      = trim($data['g_status']);
	        $map['g_unit']        = trim($data['g_unit']);
	        $map['g_type']        = 2;
	        $map['addtime']       = empty(trim($data['addtime'])) ? 0 : strtotime(trim($data['addtime']));
	        // dump($data);
	        Db::table('sz_goods')->where('id',$id)->update($map);

	        /* 商品规格信息表 */
	        for($i=0;$i<$data['length'];$i++){
		    	$cate['i_name'] = $data['i_name'.$i];	
		    	$cate['i_price'] = $data['i_price'.$i];	
		    	$cate['i_number'] = $data['i_number'.$i];	
		    	$cate['i_commission'] = $data['i_commission'.$i];

		        if(!empty($data['id'.$i])){
		        	Db::table('sz_info')->where('id',$data['id'.$i])->update($cate);
		        }else{
		        	$cate['gid'] = $id;
		        	Db::table('sz_info')->insert($cate);
		        }	
		    }
		          
	        return true;

		}else{
			return false;
		}
	}

	/**
	*	删除商品
	*/
	public function deleteGoods($id)
	{
		if($id){
			Db::table('sz_goods')->where('id',$id)->delete();
			Db::table('sz_info')->where('gid',$id)->delete();
			return true;
		}else{
			return false;
		}
	}



/*****************************************************   团购商品区域   *********************************************************************/

	/**
	*	团购商品列表
	*/
	public function getGroupGoods($data)
	{
		$where = [];

		/* 商品分类名称 */
		$cid = empty($data['cid']) ? '' : $data['cid'];
		$cid = trim($cid);
		if(!empty($cid)){
			$where['g.cid'] = $cid;
		}

		/* 商品名称 */
		$g_name = empty($data['g_name']) ? '' : $data['g_name'];
		$g_name = trim($g_name);
		if(!empty($g_name)){
			$where['g.g_name'] = $g_name;
		}

		/* 展示地方 */
		$g_show = empty($data['g_show']) ? '' : $data['g_show'];
		$g_show = trim($g_show);
		if(!empty($g_show)){
			$where['g.g_show'] = $g_show;
		}

		/* 状态 */
		$g_status = empty($data['g_status']) ? '' : $data['g_status'];
		$g_status = trim($g_status);
		if(!empty($g_status)){
			$where['g.g_status'] = $g_status;
		}

		/* 商品类型 */
		$where['g_type'] = 1;

		$join = [
			['sz_category c','g.cid=c.id','left']
		];

		$result = Db::table('sz_goods')->alias('g')
				->join($join)
				->field('g.*,c.name')
				->where($where)
				->order('g.id DESC')
				->paginate(20,false,['query' => request()->param()]);

		return $result;
	}


	/**
	*	添加团购商品
	*/
	public function addGroupGoods($data)
	{

		$map = [];
		$cate = [];
		if($data){
			if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
		        $map['g_pic']['photos'] = [];
		        foreach ($data['photo_urls'] as $key => $url) {
		            $photoUrl = cmf_asset_relative_url($url);
		            array_push($map['g_pic']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
		        }
        	}

        	/* 商品表 */
        	// 将图片的数组转为json格式，方便保存到数据库
	        $map['g_pic']         = json_encode($map['g_pic']);
	        $map['cid']           = $data['cid'];
	        $map['g_name']        = trim($data['g_name']);
	        $map['g_cprice']      = trim($data['g_cprice']);
	        $map['g_oprice']      = trim($data['g_oprice']);
	        $map['g_show']        = trim($data['g_show']);
	        $map['g_description'] = trim($data['g_description']);
	        $map['g_content']     = trim($data['g_content']);
	        $map['g_status']      = trim($data['g_status']);
	        $map['g_tnumber']     = trim($data['g_tnumber']);
	        $map['g_unit']        = trim($data['g_unit']);
	        $map['g_type']        = 1;
	        $map['addtime']       = empty($data['addtime']) ? 0 : strtotime(trim($data['addtime']));
	        $map['starttime']     = empty($data['starttime']) ? 0 : strtotime(trim($data['starttime']));
	        $map['endtime']       = empty($data['endtime']) ? 0 : strtotime(trim($data['endtime']));

	        $res = Db::table('sz_goods')->insertGetId($map);

	        /* 商品规格信息表 */

			for($i=1;$i<=$data['length'];$i++){
		    	$cate['i_name'] = $data['i_name'.$i];	
		    	$cate['i_price'] = $data['i_price'.$i];	
		    	$cate['i_number'] = $data['i_number'.$i];	
		    	$cate['i_commission'] = $data['i_commission'.$i];
		    	$cate['gid']  = $res;

		    	Db::table('sz_info')->insert($cate);	
		    }

	        return true;

		}else{
			return false;
		}
	}


	/**
	*	获取单个团购商品信息
	*/
	public function getGroupOne($id)
	{
		$join = [
			['sz_category c','g.cid=c.id','left']
		];
		$result = Db::table('sz_goods')->alias('g')
				->join($join)
				->field('g.*,c.name')
				->where('g.id',$id)
				->find();

		return $result;
	}


	/**
	*	修改单个团购商品信息提交
	*/	
	public function editGroupGoods($id,$data)
	{
		$map = [];
		$cate = [];
		if($data){
			if (!empty($data['photo_names']) && !empty($data['photo_urls'])) {
		        $map['g_pic']['photos'] = [];
		        foreach ($data['photo_urls'] as $key => $url) {
		            $photoUrl = cmf_asset_relative_url($url);
		            array_push($map['g_pic']['photos'], ["url" => $photoUrl, "name" => $data['photo_names'][$key]]);
		        }
        	}

        	/* 商品表 */
        	// 将图片的数组转为json格式，方便保存到数据库
	        $map['g_pic']         = json_encode($map['g_pic']);
	        $map['cid']           = trim($data['cid']);
	        $map['g_name']        = trim($data['g_name']);
	        $map['g_cprice']      = trim($data['g_cprice']);
	        $map['g_oprice']      = trim($data['g_oprice']);
	        $map['g_show']        = trim($data['g_show']);
	        $map['g_description'] = trim($data['g_description']);
	        $map['g_content']     = trim($data['g_content']);
	        $map['g_status']      = trim($data['g_status']);
	        $map['g_tnumber']     = trim($data['g_tnumber']);
	        $map['g_unit']        = trim($data['g_unit']);
	        $map['g_type']        = 1;
	        $map['addtime']       = empty($data['addtime']) ? 0 : strtotime(trim($data['addtime']));
	        $map['starttime']     = empty($data['starttime']) ? 0 : strtotime(trim($data['starttime']));
	        $map['endtime']       = empty($data['endtime']) ? 0 : strtotime(trim($data['endtime']));

	        Db::table('sz_goods')->where('id',$id)->update($map);
	        /* 商品规格信息表 */
	        for($i=0;$i<$data['length'];$i++){
		    	$cate['i_name'] = $data['i_name'.$i];	
		    	$cate['i_price'] = $data['i_price'.$i];	
		    	$cate['i_number'] = $data['i_number'.$i];	
		    	$cate['i_commission'] = $data['i_commission'.$i];

		        if(!empty($data['id'.$i])){
		        	Db::table('sz_info')->where('id',$data['id'.$i])->update($cate);
		        }else{
		        	$cate['gid'] = $id;
		        	Db::table('sz_info')->insert($cate);
		        }	
		    }
	        
	        return true;

		}else{
			return false;
		}
	}

	/**
	*	删除团购商品
	*/
	public function deleteGroupGoods($id)
	{
		if($id){
			Db::table('sz_goods')->where('id',$id)->delete();
			Db::table('sz_info')->where('gid',$id)->delete();
			return true;
		}else{
			return false;
		}
	}


	/**
	*	批量上架
	*/
	public function racking($data)
	{
		$aryid = rtrim($data['id'],','); // 去掉最右边的逗号
		$aryid = explode(",",$aryid);

		// 更新内容
		$map['g_status'] = 2;		
		$map['addtime'] = strtotime(date('Y-m-d H:i',time()));
 		foreach ($aryid as $key => $val) {
			$res = Db::table('sz_goods')->where('id='.$val)->update($map);		
		}
		if($res){
			return true;
		}else{
			return false;
		}
	}

	/**
	*	批量下架
	*/
	public function rack($data)
	{
		$aryid = rtrim($data['id'],','); // 去掉最右边的逗号
		$aryid = explode(",",$aryid);

		// 更新内容
		$map['g_status'] = 3;
		$map['addtime'] = strtotime(date('Y-m-d H:i',time()));

		foreach ($aryid as $key => $val) {
			$res = Db::table('sz_goods')->where('id='.$val)->update($map);		
		}
		if($res){
			return true;
		}else{
			return false;
		}
	}

	/**
	*	删除商品的规格
	*/
	public function deleteInfo($id)
	{
		if($id){
			Db::table('sz_info')->where('id',$id)->delete();
			return true;
		}else{
			return false;
		}
	}

}